/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
import com.pig4cloud.pigx.ruralLibrary.api.entity.*;
import com.pig4cloud.pigx.ruralLibrary.mapper.ObpCityMapper;
import com.pig4cloud.pigx.ruralLibrary.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${tableComment}
 *
 * @author pig
 * @date 2023-06-29 11:45:31
 */
@Service
public class TerrainServiceImpl implements terrainService {
	@Autowired
	private ObpCityService obpCityService;
	@Autowired
	private ObpAreaService obpAreaService;
	@Autowired
	private ObjStreetService objStreetService;
	@Autowired
	private ObpCommitteeService obpCommitteeService;
	@Autowired
	private ObpCityMapper obpCityMapper;
	@Autowired
	private BookUserService bookUserService;


	public R getCity() {
		return R.ok(obpCityService.list());
	}

	@Override
	public R getDistrict(String city) {
		LambdaQueryWrapper<ObpAreaEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObpAreaEntity::getCityCode, city);
		return R.ok(obpAreaService.list(wrapper));
	}

	@Override
	public R getVillage(String district) {
		LambdaQueryWrapper<ObjStreetEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObjStreetEntity::getAreaCode, district);
		return R.ok(objStreetService.list(wrapper));
	}

	@Override
	public R getCommittee(String committee) {
		LambdaQueryWrapper<ObpCommitteeEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObpCommitteeEntity::getStreetCode, committee);
		return R.ok(obpCommitteeService.list(wrapper));
	}

	@Override
	public List<terrainEntity> export(terrainEntity terrainEntity) {
		String name = SecurityUtils.getUser().getUsername();
		LambdaQueryWrapper<BookUserEntity> userwrapper = Wrappers.lambdaQuery();
		userwrapper.eq(BookUserEntity::getUserName, name);
		BookUserEntity bookUserEntity = bookUserService.getOne(userwrapper);
		if (StrUtil.isNotBlank(bookUserEntity.getJurisdiction())) {
			if (!bookUserEntity.getJurisdiction().equals("1")) {
				if (bookUserEntity.getJurisdiction().equals("2")) {
					terrainEntity.setCode(bookUserEntity.getCity());
				} else if (bookUserEntity.getJurisdiction().equals("3")) {
					terrainEntity.setCode(bookUserEntity.getDistrict());
				} else if (bookUserEntity.getJurisdiction().equals("4")) {
					terrainEntity.setCode(bookUserEntity.getVillage());
				} else if (bookUserEntity.getJurisdiction().equals("5")) {
					terrainEntity.setCode(bookUserEntity.getCommittee());
				}
			}
		}
		List<terrainEntity> all = obpCityMapper.getAll(null, terrainEntity, bookUserEntity.getJurisdiction());
		return all;
	}

	@Override
	public R removeBatchByIds(String id) {
		LambdaQueryWrapper<ObpCommitteeEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObpCommitteeEntity::getCommitteeCode, id);
		return R.ok(obpCommitteeService.remove(wrapper));
	}

	@Override
	public String getAll(String id) {
		terrainEntity one = obpCityMapper.getOne(id);
		return one.getProvName() + one.getCityName() + one.getAreaName() + one.getStreetName() + one.getCommitteeName();
	}

	@Override
	public List<terrainEntity> getAll(Page page, terrainEntity terrainEntity, String jurisdiction) {
		return obpCityMapper.getAll(page, terrainEntity, jurisdiction);
	}

	@Override
	public terrainEntity getOne(String id) {
		return obpCityMapper.getOne(id);
	}

	@Override
	public R insert(terrainEntity terrainEntity) {
		List<ObpCommitteeEntity> list = obpCommitteeService.list();
		boolean exsitUserName = list.stream()
				.anyMatch(ObpCommitteeEntity -> terrainEntity.getCommitteeCode().equals(ObpCommitteeEntity.getCommitteeCode()));
		if (exsitUserName) {
			return R.failed("重复居委会");
		}
		ObpCommitteeEntity obpCommitteeEntity = new ObpCommitteeEntity();
		BeanUtils.copyProperties(terrainEntity, obpCommitteeEntity);
		return R.ok(obpCommitteeService.save(obpCommitteeEntity));
	}

	@Override
	public R updateCommitteeName(terrainEntity terrainEntity) {
		ObpCityEntity obpCityEntity = new ObpCityEntity();
		BeanUtils.copyProperties(terrainEntity, obpCityEntity);
		LambdaQueryWrapper<ObpCityEntity> wrapper3 = Wrappers.lambdaQuery();
		wrapper3.eq(ObpCityEntity::getCityCode, obpCityEntity.getCityCode());
		obpCityService.update(obpCityEntity, wrapper3);

		ObpAreaEntity obpAreaEntity = new ObpAreaEntity();
		BeanUtils.copyProperties(terrainEntity, obpAreaEntity);
		LambdaQueryWrapper<ObpAreaEntity> wrapper2 = Wrappers.lambdaQuery();
		wrapper2.eq(ObpAreaEntity::getAreaCode, obpAreaEntity.getAreaCode());
		obpAreaService.update(obpAreaEntity, wrapper2);

		ObjStreetEntity objStreetEntity = new ObjStreetEntity();
		BeanUtils.copyProperties(terrainEntity, objStreetEntity);
		LambdaQueryWrapper<ObjStreetEntity> wrapper1 = Wrappers.lambdaQuery();
		wrapper1.eq(ObjStreetEntity::getStreetCode, objStreetEntity.getStreetCode());
		objStreetService.update(objStreetEntity, wrapper1);

		ObpCommitteeEntity obpCommitteeEntity = new ObpCommitteeEntity();
		BeanUtils.copyProperties(terrainEntity, obpCommitteeEntity);
		LambdaQueryWrapper<ObpCommitteeEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ObpCommitteeEntity::getCommitteeCode, obpCommitteeEntity.getCommitteeCode());
		return R.ok(obpCommitteeService.update(obpCommitteeEntity, wrapper));
	}


}
